package com.itheima.tanhua.vo;

import com.itheima.tanhua.mongo.RecommendUser;
import com.itheima.tanhua.domain.UserInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.util.*;

/**
 * 今日佳人
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TodayBest {

    private Long id; //用户id
    private String avatar;
    private String nickname;
    private String gender; //性别 man woman
    private Integer age;
    private String[] tags;
    private Long fateValue; //缘分值

    /**
     * 在vo对象中，补充一个工具方法，封装转化过程
     */
    public static TodayBest init(UserInfo userInfo, RecommendUser recommendUser) {
        TodayBest vo = new TodayBest();
        BeanUtils.copyProperties(userInfo, vo);
        if (userInfo.getTags() != null) {
            vo.setTags(userInfo.getTags().split(","));
        }
        vo.setFateValue(recommendUser.getScore().longValue());
        return vo;
    }

    /**
     * 在vo对象中，补充一个工具方法，封装转化过程
     */
    public static List<TodayBest> initList(List<UserInfo> userInfos, Map<Long, Double> map) {
        List<TodayBest> result = new ArrayList<>();
        for (UserInfo userInfo : userInfos) {
            TodayBest vo = new TodayBest();
            BeanUtils.copyProperties(userInfo, vo);
            if (userInfo.getTags() != null) {
                vo.setTags(userInfo.getTags().split(","));
            }
            vo.setFateValue(map.get(userInfo.getId()).longValue());
            result.add(vo);
        }
        //降序排序
        Collections.sort(result, new Comparator<TodayBest>() {
            @Override
            public int compare(TodayBest t1, TodayBest t2) {
                return (int) (t2.getFateValue() - t1.getFateValue());
            }
        });
        return result;
    }
}